package com.way.gesture;

import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureOverlayView;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.way.util.AppUtil;
import com.way.util.GestureLevel;
import com.way.util.GestureLibraryManager;

public class CreateGestureActivity extends Activity implements
		GestureOverlayView.OnGesturePerformedListener {

	private Gesture mGesture;
	private View mDoneButton;
	private GestureOverlayView mGestureOverlayView;
	private static String mGestureName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.create_gesture);
		AppUtil.transWindows(this,
				getResources().getColor(R.color.material_teal));
		getActionBar().setDisplayHomeAsUpEnabled(true);
		mDoneButton = findViewById(R.id.done);
		TextView titleView = (TextView) findViewById(R.id.gesture_name);
		String title = getIntent().getStringExtra("task");
		if (!TextUtils.isEmpty(title)) {
			mGestureName = title;
			setName(titleView, mGestureName);
		}
		mGestureOverlayView = (GestureOverlayView) findViewById(R.id.gestures_overlay);
		mGestureOverlayView.addOnGesturePerformedListener(this);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == android.R.id.home) {
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private void setName(TextView label, String name) {
		if (!name.contains(","))
			return;
		String[] names = name.split(",");
		int command = Integer.valueOf(names[0]);
		switch (command) {
		case SelectTaskActivity.APP_COMMAND:
			label.setText(String.format(
					getResources().getString(R.string.app_prex), names[3]));
			break;
		case SelectTaskActivity.DIAL_COMMAND:
			if (names.length > 1)
				label.setText(String.format(
						getResources().getString(R.string.dial_prex), names[2]));
			else
				label.setText(String.format(
						getResources().getString(R.string.dial_prex), names[1]));
			break;
		case SelectTaskActivity.MMS_COMMAND:
			if (names.length > 1)
				label.setText(String.format(
						getResources().getString(R.string.mms_prex), names[2]));
			else
				label.setText(String.format(
						getResources().getString(R.string.mms_prex), names[1]));
			break;
		default:
			break;
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);

		if (mGesture != null) {
			outState.putParcelable("gesture", mGesture);
		}
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);

		mGesture = savedInstanceState.getParcelable("gesture");
		restoreGesture();
	}

	private void restoreGesture() {
		if (mGesture == null)
			return;
		final GestureOverlayView overlay = (GestureOverlayView) findViewById(R.id.gestures_overlay);
		overlay.post(new Runnable() {
			public void run() {
				overlay.setGesture(mGesture);
			}
		});

		mDoneButton.setEnabled(true);
	}

	@SuppressWarnings({ "UnusedDeclaration" })
	public void addGesture(View v) {
		if (mGesture == null)
			return;
		final CharSequence name = mGestureName;
		if (name.length() == 0) {
			return;
		}
		Log.i("liweiping", "addGesture = " + name.toString());
		final GestureLibraryManager store = GestureLibraryManager
				.getInstance(this);
		if (TextUtils.equals(getIntent().getAction(), "com.way.edit"))
			store.removeGesture(name.toString());
		store.updateGesture(name.toString(), mGesture);

		Toast.makeText(this, R.string.save_success, Toast.LENGTH_LONG).show();
		finish();

	}

	@SuppressWarnings({ "UnusedDeclaration" })
	public void cancelGesture(View v) {
		mGesture = null;
		mGestureOverlayView.clear(true);
		mDoneButton.setEnabled(false);
	}

	Runnable recognizeTask = new Runnable() {

		@Override
		public void run() {
			recognizeGesture();
		}
	};

	public void recognizeGesture() {
		GestureLibraryManager gestureLibraryManager = GestureLibraryManager
				.getInstance(this);
		String result = gestureLibraryManager.searchGesture(mGesture);
		if (!TextUtils.isEmpty(result)) {
			mGesture = null;
			Toast.makeText(CreateGestureActivity.this,
					R.string.gesture_exist_toast, Toast.LENGTH_SHORT).show();
			return;
		}
		restoreGesture();
		mDoneButton.setEnabled(true);
	}

	@Override
	public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
		if (gesture == null)
			return;
		if (gesture.getLength() < GestureLevel.MIN_LENGTH) {
			Toast.makeText(CreateGestureActivity.this,
					R.string.gesture_message_too_short, Toast.LENGTH_SHORT)
					.show();
			return;
		}
		if (gesture.getLength() > GestureLevel.MAX_LENGTH) {
			Toast.makeText(CreateGestureActivity.this,
					R.string.gesture_message_too_long, Toast.LENGTH_SHORT)
					.show();
			return;
		}
		mGesture = gesture;
		// 检测是否存在此手势
		CreateGestureActivity.this.runOnUiThread(recognizeTask);
	}
}
